package com.bowen.security.rsa.upstream;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bowen.security.rsa.model.AuthRequest;
import com.bowen.security.rsa.util.RsaUtil;
import com.bowen.test.TestUpStreamModel;
import com.bowen.util.RandomUtils;

/**
 * 上游系统操作端 包括秘钥和加密
 * 
 * @author Bowen
 * @since 2021-12-03 17:12:20
 */
public class UpStreamClient implements com.bowen.security.rsa.Client {

  // 设置为单例模式
  private volatile static UpStreamClient upStreamClient;

  private String downStreamPublicKey;
  private String upStreamPrivateKey;

  private UpStreamClient() {

  }

  /**
   * 创建上游系统实例
   * 
   * @return UpStreamClient
   */
  public static UpStreamClient getUpStreamClient() {
    if (upStreamClient == null) {
      synchronized (UpStreamClient.class) {
        if (upStreamClient == null) {
          upStreamClient = new UpStreamClient();
        }
      }
    }
    return upStreamClient;
  }

  /**
   * 加密
   * 
   * @param <T>           继承AuthRequest
   * @param data          数据
   * @param downPublicKey 下游公钥
   * @param upPrivateKey  上游私钥
   * @return String加密串
   */
  @Override
  public <T extends AuthRequest> String encryption(T data, String downPublicKey, String upPrivateKey) {
    return RsaUtil.encryption(data, upPrivateKey, downPublicKey);
  }

  /**
   * 加密
   * 
   * @param <T>  继承AuthRequest
   * @param data 数据
   * @return 加密串
   */
  @Override
  public <T extends AuthRequest> String encryption(T data) {
    return RsaUtil.encryption(data, UpStreamClient.getUpStreamClient().getUpStreamPrivateKey(),
        UpStreamClient.getUpStreamClient().getDownStreamPublicKey());
  }

  public static void main(String[] args) {
    try {
      // 入参测试

      // 1.入参封装
      TestUpStreamModel res = RandomUtils.setPro(TestUpStreamModel.class);
      res.setAppid("123456");
      JSONObject jsonObject = JSON.parseObject(JSONObject.toJSONString(res));
      System.out.println(jsonObject.toJSONString());
      // 2.加密
      String encryptionStr = RsaUtil.encryption(res, "123456");
      System.out.println(encryptionStr);
      // 3.解密
      TestUpStreamModel decrypt = RsaUtil.decrypt(TestUpStreamModel.class, encryptionStr);
      System.out.println(JSONObject.toJSONString(decrypt));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 上游没有解密
   */
  @Override
  public <T extends AuthRequest> T decrypt(Class<T> clazz, String data) {
    return null;
  }

  public String getDownStreamPublicKey() {
    return downStreamPublicKey;
  }

  /**
   * 保存下游系统公钥
   * 
   * @param downStreamPublicKey 下游公钥
   * @return UpStreamClient.class
   */
  public UpStreamClient setDownStreamPublicKey(String downStreamPublicKey) {
    this.downStreamPublicKey = downStreamPublicKey;
    return this;
  }

  public String getUpStreamPrivateKey() {
    return upStreamPrivateKey;
  }

  /**
   * 保存上游系统私钥
   * 
   * @param upStreamPrivateKey 上游私钥
   * @return UpStreamClient.class
   */
  public UpStreamClient setUpStreamPrivateKey(String upStreamPrivateKey) {
    this.upStreamPrivateKey = upStreamPrivateKey;
    return this;
  }

}
